<?php

/*
 * C订单作功能模块
 * 功能：下单支付
 */

namespace Pay\Controller;

use Think\Controller;

class PayController extends Controller {

    private $app_id = '';
    private $app_secret = '';

    public function __construct() {
        parent::__construct();
        import('Api2', 'weixin');
        $api = new \Weixin\Api2();
        $this->app_id = WEIXIN_PAY_APP_ID;
        $this->app_secret = WEIXIN_PAY_SECRET;
        if (!$_SESSION['system']['open_id']) {
            $api->getUserToken('system', $this->app_id, $this->app_secret);
        }
        $store_id = (int) $_GET['store_id'];
    }

    public function index() {
        //发送短信时用
        import('MsgCode', 'Msgcode');
        $m = new \Msgcode\MsgCode();
        $_SESSION['s_verify'] = time();
        //$m->setSession($_SESSION);
        //

        $model_type = trim( $_GET['model_type'] );

        $order_number = trim($_GET['order_number']);
        $price = (int) $_GET['price'];
        if (strlen($order_number) < 1 || $price < 1) {
            exit;
        }
        $o = new \Home\Logic\OrderLogic();
        $order = $o->getOrder($order_number);

        if ($order['id'] < 1 || $price != $order['price']) {
            exit;
        }

        include_once(APP_PATH . "Weixin/pay/demo/WxPayPubHelper/WxPayPubHelper.php");
        include_once(APP_PATH . "Weixin/pay/demo/demo/log_.php");
        $jsApi = new \JsApi_pub();
        if (!isset($order['id'])) {
            //触发微信返回code码
            $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
            Header("Location: $url");
        }
        $price = $order['price'];
        //=========步骤2：使用统一支付接口，获取prepay_id============
        //使用统一支付接口
        $unifiedOrder = new \UnifiedOrder_pub();
        $_SESSION_ = json_decode(file_get_contents(SESSION_DIR.session_id()),true);
        //设置统一支付接口参数
        $unifiedOrder->setParameter("openid", "{$_SESSION_['system']['open_id']}"); //商品描述
        if($order['team_number']){
            if ( $model_type == 1 )
            {
                $unifiedOrder->setParameter("body", '拼团'); //商品描述
            }
            else if ( $model_type == 3 )
            {   
                $this->assign('headimg',trim($_GET['headimg']));  //微信头像
                $this->assign('nickname',trim($_GET['nickname']));    //微信名

                $unifiedOrder->setParameter("body", '公益'); //商品描述
            }

        }else{
            $unifiedOrder->setParameter("body", '商城订单'); //商品描述
        }
        
        //自定义订单号，此处仅作举例
        $timeStamp = $_SERVER['REQUEST_TIME'];
        //$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
        $unifiedOrder->setParameter("out_trade_no", $order['order_number']); //商户订单号 
        $unifiedOrder->setParameter("timeStamp", $timeStamp); //交易起始时间
        //$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
        $unifiedOrder->setParameter("total_fee", $order['price']); //总金额
        $unifiedOrder->setParameter("notify_url", \WxPayConf_pub::NOTIFY_URL); //通知地址 
        $unifiedOrder->setParameter("trade_type", "JSAPI"); //交易类型
        //非必填参数，商户可根据实际情况选填
        $prepay_id = $unifiedOrder->getPrepayId();
        //=========步骤3：使用jsapi调起支付============
        $jsApi->setPrepayId($prepay_id);
        $jsApiParameters = $jsApi->getParameters();
        $this->assign("jsApiParameters", $jsApiParameters);
        $this->assign( 'order',$order );
        $this->assign( 'model_type',$model_type );
        layout(false);
        $o->updateOrder($order_number, array('pay_open_id' => $_SESSION_['system']['open_id']));
        $this->display('pay');
    }

}
